package com.station.Activitys.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.station.Activitys.R;
import com.station.Activitys.utils.DataCleanUtils;
import com.station.Activitys.view.MasterLayout;
import com.zhy.autolayout.config.AutoLayoutConifg;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：
 * 类描述：清除缓存dialog
 * 创建人：张飞祥
 * 创建时间：2016/10/19 0019 下午 3:17
 * 修改人：张飞祥
 * 修改时间：2016/10/19 0019 下午 3:17
 * 修改备注：
 */
public abstract class ClearCacheDialog extends Dialog {

    static MasterLayout clearProgress;
    @Bind(R.id.clear_cache_text)
    TextView clearCacheText;
    @Bind(R.id.btn_clear_confirm)
    Button btnClearConfirm;


    private static Handler handler;
    private Context context;

    //dialog Style
    private static int mTheme = R.style.dialogDefaultTheme;
    public ClearCacheDialog(Context myContext) {
        super(myContext,mTheme);
        this.context=myContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_clear_cache);
        ButterKnife.bind(this);
        AutoLayoutConifg.getInstance().useDeviceSize();
        clearProgress=(MasterLayout) findViewById(R.id.clear_progress);
        initUI();
    }

    private void initUI() {
        clearProgress.animation();
        new DownLoadSigTask().execute();
        clearCacheText.setText("正在清理...");
        /**
         *
         * 清理内部应用所有缓存 + SharedPreference
         *
         */
        DataCleanUtils.clearAllCache(context);

        btnClearConfirm.setClickable(false);
        handler=new Handler(){
            public void handleMessage(Message msg) {
                //清理完成
                setClearComplete();
                clearCacheText.setText("清理完成");
                clearCacheText.setTextColor(Color.parseColor("#004ea1"));
                btnClearConfirm.setClickable(true);
                btnClearConfirm.setTextColor(Color.parseColor("#004ea1"));
                btnClearConfirm.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        //销毁当前页面
                        dismiss();
                    }
                });
            };
        };
    }

    public static Handler gethandler(){
        return handler;
    }

    public abstract void setClearComplete();

    //开启异步线程启动动画
    static class DownLoadSigTask extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
        }
        @Override
        protected String doInBackground(final String... args) {
            for (int i = 0; i <= 100; i++) {
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
                publishProgress(i);
            }
            return null;
        }
        @Override
        protected void onProgressUpdate(Integer... progress) {
            clearProgress.cusview.setupprogress(progress[0]);
        }
    }
}
